<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Traveloptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('traveloption');
	}
	public function ajax_traveloption_get(){
		$traveloptionEntity =  new CI_Travel_option_entity();
		$traveloption = $traveloptionEntity->getTravelOptionByCode($_REQUEST['code']);
		echo json_encode($traveloption);
	}
	public function ajax_traveloption_list(){
		$traveloptionEntity =  new CI_Travel_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$traveloptionList = $traveloptionEntity->getTravelOptionListByLanguage($defaultLanguageId);
		$data['traveloptionList'] = $traveloptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/traveloptions/index', $data);
	}
	public function ajax_traveloption_save(){
		$traveloptionNameList = $_REQUEST['traveloption'];
		$traveloptionNameDefault = '';
		foreach ($traveloptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$traveloptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($traveloptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_traveloption_require');
		}else{
			$code = 0;
			foreach ($traveloptionNameList as $key => $value) {
				$traveloptionEntity =  new CI_Travel_option_entity();
				if($value != null && $value != ''){
					$traveloptionEntity->setOption($value);
				}else{
					$traveloptionEntity->setOption($traveloptionNameDefault);
				}
				$traveloptionEntity->setLanguageId($key);
				$traveloptionEntity->setCode($code);
				$id = $traveloptionEntity->save();
				if($code == 0){
					$code = $id;
					$traveloptionEntity->setId($id);
					$traveloptionEntity->setCode($code);
					$traveloptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_traveloption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_traveloption_update(){
		$traveloptionNameList = $_REQUEST['traveloption'];
		$traveloptionIdList = $_REQUEST['traveloption_id'];
		$traveloptionCode = $_REQUEST['traveloption_code'];
		$traveloptionNameDefault = '';
		foreach ($traveloptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$traveloptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($traveloptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_traveloption_require');
		}else{
			foreach ($traveloptionNameList as $key => $value) {
				$traveloptionEntity =  new CI_Travel_option_entity();
				if($value != null && $value != ''){
					$traveloptionEntity->setOption($value);
				}else{
					$traveloptionEntity->setOption($traveloptionNameDefault);
				}
				$traveloptionEntity->setLanguageId($key);
				$traveloptionEntity->setCode($traveloptionCode);
				$traveloptionEntity->setId($traveloptionIdList[$key]);
				$traveloptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_traveloption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_traveloption_delete(){
		$result = array();
		$traveloptionEntity =  new CI_Travel_option_entity();
		$result['result'] = $traveloptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_traveloption_delete_success');
		}else{
			$result['message'] = lang('escort_traveloption_delete_error');
		}
		echo json_encode($result);
	}
}
?>